#encoding=utf-8
__author__ = 'Soul'

from DbOperator import *

class ChinaCarDbOperator(DbOperator):
    def __del__(self):
        self.close()

    def insert_record(self, record):
        # pic = topic['pic']                      # picture
        # zwpp = topic['zwpp']                    # 中文品牌
        # clxh = topic['clxh']                    # 车辆型号
        # clmc = topic['clmc']                    # 车辆名车
        # mz = topic['mz']                        # 免征
        # ry = topic['ry']                        # 燃油
        # hb = topic['hb']                        # 环保
        # dpsb = topic['dpsb']                    # 底盘商标
        # dpxh = topic['dpxh']                    # 底盘型号
        # pc = topic['pc']                        # 批次
        # zs = topic['zs']                        # 轴数
        # zj = topic['zj']                        # 轴距
        # fdjxh = topic['fdjxh']                  # 发动机型号
        # fdjcs = topic['fdjcs']                  # 发动机厂商
        # rlzl = topic['rlzl']                    # 燃料种类
        # wxcc = topic['wxcc']                    # 外形尺寸
        # edzzl = topic['edzzl']                  # 额定质量
        # ckbj = topic['ckbj']                    # 参考报价
        # tarid = topic['tarid']                  #
        # tarid_nobase = topic['tarid_nobase']    #
        # invalid = topic['invalid']              #
        # tarpic = topic['tarpic']                #

        prefix = "cecar_"
        cols = ["pic", "zwpp", "clxh", "clmc", "mz", "ry", "hb", "dpsb",
                "dpxh", "pc", "zs", "zj", "fdjxh", "fdjcs", "rlzl", "wxcc",
                "edzzl", "ckbj", "tarid", "tarid_nobase", "invalid", "tarpic"]
        coln_list= []
        val_list = []
        for key in cols:
            val = ""
            try:
                val = record[key]
                if type(val) == type(False):
                    val = str(val)
                # handle ' in string
                if "'" in val:
                    val = val.replace("'", "\"")

                val = "'" + val + "'"
            except Exception as e:
                print e
                val = ""
            finally:
                coln_list.append(prefix + key)
                val_list.append(val)

        coln_titles = ",".join(coln_list)
        values = ",".join(val_list)

        sql = "INSERT INTO spd_cecar_info(%s) VALUES(%s)"%(coln_titles, values)
        last_id = self.__do_insert__(sql)
        return last_id

    def update_detail(self, tarid, detail):
        sql = "UPDATE spd_cecar_info SET cecar_jsoninfo = '%s' WHERE cecar_tarid='%s'"%(detail, tarid)
        last_id = self.__do_update__(sql)
        return last_id